<?php

namespace app\api\model;

use think\Model;


class PaperStudentList extends Model
{

    // 表名
    protected $name = 'exam_paper_student_list';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];


    public static function getClassArray($where,$multiplejson=[],$type=0)
    {
        $schoolTotal = [];
        switch ($type) {
            case '1':
                $schoolTotal['className'] = db('school_class')->where('id', $where['class_id'])->value('name');
                //获取班主任id
                $userId = db('user_role')->where(['group_id' => 3])->where("FIND_IN_SET(" . $where['class_id'] . ",gclass_ids)")->value('user_id');
                $schoolTotal['teacherName'] = db('user')->where('id', $userId)->value('nickname');
                break;

            default:
                $schoolTotal['className'] = '学校全体';
                $schoolTotal['teacherName'] = '--';
                break;
        }
        //排名
        $schoolTotal['averageRank'] = null;
        $exams_list_id = db('exam_list')->where(['exams_id'=>$where['exams_id'],'subject_id'=>$where['subject_id']])->value('id');
        //参考人数
        // $studentNum = db('exam_paper_student_list')->where($where)->count();
        $studentNum = db('exam_paper_images')->where(['exams_list_id'=>$exams_list_id,'page'=>1])->where(function($query) use($where){
            if(isset($where['class_id'])){
                $query->where(['class_id'=>$where['class_id']]);
            }
        })->group('student_id')->count();
        $schoolTotal['studentNum'] = $studentNum;
        //全校总得分
        $schoolScore = db('exam_paper_student_list')->where($where)->sum('totalscore');
        //平均分
        $averageScore = 0;
        if ($studentNum > 0) {
            //平均分
            $averageScore = getAverage($schoolScore, $studentNum);
        }
        $schoolTotal['averageScore'] = $averageScore;
        
        //考试满分
        $examFullScore = db('exam_paper_question')->where(['exams_list_id'=> $exams_list_id])->sum('zscore');
        //平均得分率
        $averageScoreRate = 0;
        if ($examFullScore > 0) {
            //平均得分率
            $averageScoreRate = getAverageScoring($averageScore, $examFullScore);
        }
        $schoolTotal['averageScoreRate'] = $averageScoreRate;
        //高分人数
        $highNum = 0;
        //优秀人数
        $excellenceNum = 0;
        //良好人数
        $goodNum = 0;
        //及格人数
        $passNum = 0;
        //低分人数
        $lowNum = 0;
        $rates = [];
        $paperStudent = db('exam_paper_student_list')->where($where)->field('totalscore')->select();
        
        foreach ($paperStudent as $key => $rate) {
            $scoreRate = 0;
            if ($examFullScore > 0) {
                $scoreRate = getScoring($rate['totalscore'], $examFullScore);
            }
            foreach ($multiplejson as $multiple) {

                if ($scoreRate >= $multiple['low'] && $scoreRate <= $multiple['high']) {
                    switch ($multiple['name']) {
                        case '高分':
                            $highNum = bcadd($highNum, 1, 0);
                            break;
                        case '优秀':
                            $excellenceNum = bcadd($excellenceNum, 1, 0);
                            break;
                        case '良好':
                            $goodNum = bcadd($goodNum, 1, 0);
                            break;
                        case '及格':
                            $passNum = bcadd($passNum, 1, 0);
                            break;
                        case '低分':
                            $lowNum = bcadd($lowNum, 1, 0);
                            break;
                    }
                }
            }
        }
        $multipleCount = count($multiplejson);
        switch ($multipleCount) {
            case '5':
                //高分率
                $highScale = 0;
                if ($highNum > 0 && $studentNum > 0) {
                    $highScale = getRates($highNum, $studentNum);
                }
                $high = [
                    'name' => '高分',
                    'num' => $highNum,
                    'scale' => $highScale,
                    'rank' => null

                ];
                array_push($rates, $high);
                //优秀率
                $excellenceScale = 0;
                if ($excellenceNum > 0 && $studentNum > 0) {
                    $excellenceScale = getRates($excellenceNum, $studentNum);
                }
                $excellence = [
                    'name' => '优秀',
                    'num' => $excellenceNum,
                    'scale' => $excellenceScale,
                    'rank' => null

                ];
                array_push($rates, $excellence);
                //良好率
                $goodScale = 0;
                if ($goodNum > 0 && $studentNum > 0) {
                    $goodScale = getRates($goodNum, $studentNum);
                }
                $good = [
                    'name' => '良好',
                    'num' => $goodNum,
                    'scale' => $goodScale,
                    'rank' => null
                ];
                array_push($rates, $good);
                //及格率
                $passScale = 0;
                if ($passNum > 0 && $studentNum > 0) {
                    $passScale = getRates($passNum, $studentNum);
                }
                $pass = [
                    'name' => '及格',
                    'num' => $passNum,
                    'scale' => $passScale,
                    'rank' => null
                ];
                array_push($rates, $pass);

                //低分率
                $lowScale = 0;
                if ($lowNum > 0 && $studentNum > 0) {
                    $lowScale = getRates($lowNum, $studentNum);
                }
                $low = [
                    'name' => '低分',
                    'num' => $lowNum,
                    'scale' => $lowScale,
                    'rank' => null
                ];
                array_push($rates, $low);
                break;
            case '4':
                //优秀率
                $excellenceScale = 0;
                if ($excellenceNum > 0 && $studentNum > 0) {
                    $excellenceScale = getRates($excellenceNum, $studentNum);
                }
                $excellence = [
                    'name' => '优秀',
                    'num' => $excellenceNum,
                    'scale' => $excellenceScale,
                    'rank' => null

                ];
                array_push($rates, $excellence);
                //良好率
                $goodScale = 0;
                if ($goodNum > 0 && $studentNum > 0) {
                    $goodScale = getRates($goodNum, $studentNum);
                }
                $good = [
                    'name' => '良好',
                    'num' => $goodNum,
                    'scale' => $goodScale,
                    'rank' => null
                ];
                array_push($rates, $good);
                //及格率
                $passScale = 0;
                if ($passNum > 0 && $studentNum > 0) {
                    $passScale = getRates($passNum, $studentNum);
                }
                $pass = [
                    'name' => '及格',
                    'num' => $passNum,
                    'scale' => $passScale,
                    'rank' => null
                ];
                array_push($rates, $pass);

                //低分率
                $lowScale = 0;
                if ($lowNum > 0 && $studentNum > 0) {
                    $lowScale = getRates($lowNum, $studentNum);
                }
                $low = [
                    'name' => '低分',
                    'num' => $lowNum,
                    'scale' => $lowScale,
                    'rank' => null
                ];
                array_push($rates, $low);
                break;
            case '3':
                //优秀率
                $excellenceScale = 0;
                if ($excellenceNum > 0 && $studentNum > 0) {
                    $excellenceScale = getRates($excellenceNum, $studentNum);
                }
                $excellence = [
                    'name' => '优秀',
                    'num' => $excellenceNum,
                    'scale' => $excellenceScale,
                    'rank' => null

                ];
                array_push($rates, $excellence);
                //良好率
                $goodScale = 0;
                if ($goodNum > 0 && $studentNum > 0) {
                    $goodScale = getRates($goodNum, $studentNum);
                }
                $good = [
                    'name' => '良好',
                    'num' => $goodNum,
                    'scale' => $goodScale,
                    'rank' => null
                ];
                array_push($rates, $good);
                //及格率
                $passScale = 0;
                if ($passNum > 0 && $studentNum > 0) {
                    $passScale = getRates($passNum, $studentNum);
                }
                $pass = [
                    'name' => '及格',
                    'num' => $passNum,
                    'scale' => $passScale,
                    'rank' => null
                ];
                array_push($rates, $pass);
                break;
        }
        $schoolTotal['rates'] = $rates;
        //综合率
        $multipleRate = getCompositeRate($averageScoreRate, $excellenceScale, $passScale);
        $schoolTotal['multipleRate'] = $multipleRate;
        $schoolTotal['multipleRank'] = null;
        //标准差
        $examScore = db('exam_paper_student_list')->where($where)->column('totalscore');
        $standardDeviation = 0;
        if (!empty($examScore)) {
            $standardDeviation = getStandardDeviation($examScore);
        }
        $schoolTotal['standardDeviation'] = $standardDeviation;
        //最高
        $max = db('exam_paper_student_list')->where($where)->max('totalscore');
        $schoolTotal['max'] = $max;
        //最低
        $min = db('exam_paper_student_list')->where($where)->min('totalscore');
        $schoolTotal['min'] = $min;

        return $schoolTotal;
    }


    //分段统计
    public static function sectionTotal($where,$criterionNum=50,$low=0,$high,$type=0,$arrayType = 1)
    {
        $schoolTotal = [];
        switch ($arrayType) {
            case '1':
                $schoolTotal['className'] = db('school_class')->where('id', $where['class_id'])->value('name');
                break;
            default:
                $schoolTotal['className'] = '学校全体';
                break;
        }
        $exams_list_id = db('exam_list')->where(['exams_id'=>$where['exams_id'],'subject_id'=>$where['subject_id']])->value('id');
        
        //参考人数
        // $studentNum = db('exam_paper_student_list')->where($where)->count();
        $studentNum = db('exam_paper_images')->where(['exams_list_id'=>$exams_list_id,'page'=>1])->where(function($query) use($where){
            if(isset($where['class_id'])){
                $query->where(['class_id'=>$where['class_id']]);
            }
        })->group('student_id')->count();
        $schoolTotal['studentNum'] = $studentNum;//参考人数
        //计算可分段个数
        $sectionNum = round(($high-$low)/$criterionNum,0);
        $mod = ($high-$low)%$criterionNum;
        if ($mod > 0) {
            $sectionNum = $sectionNum+1;
        }
        //区间
        $sectionList = getSection(intval($high), $sectionNum, intval($low),intval($criterionNum));
        $segments = [];
        switch ($type) {
            case '1':
                #累计统计
                foreach ($sectionList as $key => $value) {
                    //人数
                    $paperNum =  db('exam_paper_student_list')->where($where)->where('totalscore','>=',$value[0])->count();
                    //比例
                    $scale = 0;
                    if ($studentNum > 0) {
                        $scale = round($paperNum/$studentNum,2);
                    }
                    $array = [
                        'name' => $value[0],
                        'num' => $paperNum,
                        'scale' => $scale
                    ];
                    array_push($segments, $array);
                }
                break;
            
            default:
                #分段统计
                foreach ($sectionList as $key => $value) {
                    //人数
                    $paperNum =  db('exam_paper_student_list')->where($where)->whereBetween('totalscore',$value)->count();
                    //比例
                    $scale = 0;
                    if ($studentNum > 0) {
                        $scale = round($paperNum/$studentNum,2);
                    }
                    $array = [
                        'name' => implode(',',$value),
                        'num' => $paperNum,
                        'scale' => $scale
                    ];
                    array_push($segments, $array);
                }
                
                break;
        }
        $schoolTotal['segments'] = $segments;
        return $schoolTotal;
        
    }

    //名次和比例分析
    public static function rankScale($where,$setId,$type=0)
    {
        $set =  db('analyse_set')->where(['id' => $setId])->find();
        if(!$set) return [];
        $data = [];
        $multiplejson = json_decode($set['multiplejson'], true);
        foreach ($multiplejson as $key => $value) {
            $data[] = $key;
        }
        $schoolTotal = [];
        switch ($type) {
            case '1':
                $schoolTotal['className'] = db('school_class')->where('id', $where['class_id'])->value('name');
                break;
            default:
                $schoolTotal['className'] = '学校全体';
                break;
        }
        $exams_list_id = db('exam_list')->where(['exams_id'=>$where['exams_id'],'subject_id'=>$where['subject_id']])->value('id');
        //参考人数
        // $studentNum = db('exam_paper_student')->where($where)->count();
        $studentNum = db('exam_paper_images')->where(['exams_list_id'=>$exams_list_id,'page'=>1])->where(function($query) use($where){
            if(isset($where['class_id'])){
                $query->where(['class_id'=>$where['class_id']]);
            }
        })->group('student_id')->count();
        $schoolTotal['studentNum'] = $studentNum;//参考人数
       
        $ranks = [];
        switch ($set['type']) {
            case '1':
                #按名次统计
                foreach ($data as $key => $value) {
                    //人数
                    $paperNum =  db('exam_paper_student_list')->where($where)->where('schoolrank','<=',$value)->count();
                    //比例
                    $scale = 0;
                    if ($studentNum > 0) {
                        $scale = round($paperNum/$studentNum,2);
                    }
                    $array = [
                        'name' => $value,
                        'num' => $paperNum,
                        'scale' => $scale
                    ];
                    array_push($ranks, $array);
                }
                break;
            case '2':
                #按分数统计
                foreach ($data as $key => $value) {
                    //人数
                    $paperNum =  db('exam_paper_student_list')->where($where)->where('totalscore','>=',$value)->count();
                    //比例
                    $scale = 0;
                    if ($studentNum > 0) {
                        $scale = round($paperNum/$studentNum,2);
                    }
                    $array = [
                        'name' => $value,
                        'num' => $paperNum,
                        'scale' => $scale
                    ];
                    array_push($ranks, $array);
                }
                break;
            case '3':
                #按比例统计
                foreach ($data as $key => $value) {
                    //考试总分
                    $examScore = db('exam_paper_question')->where(['exams_list_id'=>$exams_list_id])->sum('score');
                    
                    //比例
                    $rate = (100-$value)/100;
                    //前n%分数
                    $score = round($examScore*$rate,2);
                    
                    //人数
                    $paperNum =  db('exam_paper_student_list')->where($where)->where('totalscore','>=',$score)->count();
                    
                    //比例
                    $scale = 0;
                    if ($studentNum > 0) {
                        $scale = round($paperNum/$studentNum,2);
                    }
                    $array = [
                        'name' => $value,
                        'num' => $paperNum,
                        'scale' => $scale
                    ];
                    array_push($ranks, $array);
                }
                break;
        }
        $schoolTotal['ranks'] = $ranks;
        return $schoolTotal;
        
    }
}
